//
//  RootViewController.m
//  扫一扫添加好友
//
//  Created by 企点点 on 15/8/13.
//  Copyright (c) 2015年 企点点. All rights reserved.
//

#import "RootViewController.h"
#import "ZBarSDK.h"
#import "QRCodeViewController.h"
#import "QRCodeGenerator.h"
@interface RootViewController ()<ZBarReaderDelegate,UITextFieldDelegate>
// 如果引入   生成二维码文件  有报错比较多的话

// 做已下操作

//1.单击你的工程，选择TARGET下的目标文件
//
//2.在选项卡中选择“Build Settings”选项
//
//3.找到“Apple LLVM compiler x.x - Language”设置项（其中x.x为你的xcode默认的LLVM编译器版本，不同的xcode版本不一样）
//
//4.图中绿色选中的选项“Compile Sources As”，意思是要把工程按照哪一种语言进行编译，默认是第一个“According to File Type”，将其改成Objective-C++即可。

// >>>>>> 或者在 QRCodeGenerator.h 文件中引入  #import <UIKit/UIKit.h>



// 生成二维码按钮
@property (nonatomic, strong) UIButton *QRButton;

@property (nonatomic, strong) UIImageView *ORView;

@property (nonatomic, strong) UITextField *fieldText;

@end

@implementation RootViewController


// 懒加载
- (UIButton *)QRButton {
    if (!_QRButton) {
        self.QRButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _QRButton.frame = CGRectMake(0, 0, 100, 30);
        
        [_QRButton setTitle:@"生成二维码" forState:UIControlStateNormal];
        [_QRButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [_QRButton addTarget:self action:@selector(handleQRButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _QRButton;
}


// 显示生成二维码的图片
- (UIImageView *)ORView {
    if (!_ORView) {
        self.ORView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 0, self.view.bounds.size.width - 40, self.view.bounds.size.height * 0.5)];
        self.ORView.backgroundColor = [UIColor whiteColor];
        self.ORView.center = self.view.center;
    }
    return _ORView;
}
//
- (UITextField *)fieldText {
    if (!_fieldText) {
        self.fieldText = [[UITextField alloc] initWithFrame:CGRectMake(10, 66, self.view.bounds.size.width - 20, 30)];
        self.fieldText.borderStyle = UITextBorderStyleRoundedRect;
    }
    return _fieldText;
}



// 生成二维码的方法
- (void)handleQRButton:(UIButton *)sender {
    
    /*字符转二维码
     导入 libqrencode文件
     引入头文件#import "QRCodeGenerator.h" 即可使用
     */
    
    self.ORView.image = [QRCodeGenerator qrImageForString:self.fieldText.text imageSize:self.ORView.bounds.size.width - 20];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.fieldText];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
    
    // 按钮的响应方法 *******点击按钮扫描二维码*********
    
    [button addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.QRButton];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
    [self.view addSubview:self.ORView];
    // 设置代理对象
    self.fieldText.delegate = self;
}

// 按钮点击后的响应方法
- (void)handleButton:(UIButton *)sender {
    
    if ([self validateCamera]) {
        [self sysButtonClick];
        // [self showQRViewController];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"没有可用摄像头或摄像头不可用" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
    }
    
}

//二维码扫描
- (void)sysButtonClick{
    
    /*扫描二维码部分：
     导入ZBarSDK文件并引入一下框架
     AVFoundation.framework
     CoreMedia.framework
     CoreVideo.framework
     QuartzCore.framework
     libiconv.dylib
     引入头文件#import “ZBarSDK.h” 即可使用
     当找到条形码时，会执行代理方法
     
     - (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info
     
     最后读取并显示了条形码的图片和内容。*/
    
    ZBarReaderViewController *reader = [ZBarReaderViewController new];
    reader.readerDelegate = self;
    reader.view.superview.frame = CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64);
    reader.view.superview.center = self.view.center;
    ZBarImageScanner *scanner = reader.scanner;

    [scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0];
    [self presentViewController:reader animated:YES completion:nil];
}



- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    //得到条形码
    id<NSFastEnumeration>resuls = [info objectForKey:ZBarReaderControllerResults];
    ZBarSymbol *symbol = nil;
    for (symbol in resuls) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:symbol.data delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
        break;
    }
    
}



-(BOOL)validateCamera
{
    return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] && [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
}

// 收回键盘

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
